//
//  ViewController.m
//  03-17练习-hitTest
//
//  Created by student on 16/3/17.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
{
    BOOL bMove;
    CGPoint pointXY;//距离中心点的偏移
}
@property (weak, nonatomic) IBOutlet UIButton *button;
@property (weak, nonatomic) IBOutlet UIView *moveView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIPanGestureRecognizer *panR=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(moveV:)];
    [self.moveView addGestureRecognizer:panR];
   
}

//方式二:手势

-(void)moveV:(UIPanGestureRecognizer *)g
{
    //偏移多少
    
    CGPoint tran=[g translationInView:self.moveView];
    
    //加上偏移
    
    self.moveView.center=CGPointMake(self.moveView.center.x+tran.x, self.moveView.center.y+tran.y);
    
    //重置偏移量
    
    [g setTranslation:CGPointZero inView:self.moveView];
    
    
    
}

//方式1；touchesBegan
//- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event
//{
//    NSLog(@"%s",__func__);
//    UITouch *touch=[touches anyObject];
//    CGPoint point=[touch locationInView:self.view];
//    
//    //1.得到触摸点是否在moveView范围内
//    bMove=CGRectContainsPoint(self.moveView.frame, point);
//    
//    if (bMove) {
//        //计算触摸点距离中心点的偏移
//        pointXY=CGPointMake(point.x-self.moveView.center.x, point.y-self.moveView.center.y);
//        
//        
//    }
//}
//- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event
//{
//    if (bMove) {
//        UITouch *touch=[touches anyObject];
//        CGPoint point=[touch locationInView:self.view];
//        //2.移动它
//        //触摸点-偏移
//        self.moveView.center=CGPointMake(point.x-pointXY.x, point.y-pointXY.y);
//        
//    }
//}
//- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event
//{
//    //3.结束移动
//    bMove=NO;
//}
//

@end
